package com.fairytask.engine.components.autoPullUpList
{
	import com.adobe.utils.StringUtil;
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.event.AutoPullUpListEvent;
	import com.fairytask.managers.ResManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 * 自适应拉升列表(任务模块使用) 
	 * @author zhuy
	 * 
	 */	
	public class AutoPullUpList extends BaseComponent
	{
		//属性
		private var _rootItemBg	:String;//根项背景
		private var _childItemBg	:String;//子项背景
		private var _overResStr	:String;//鼠标移上背景
		private var _clickResStr	:String;//鼠标点击背景
		//显示
		private var _treeSpace		:Sprite		=	new Sprite();//列表显示区域
		private var _clickRes		:MovieClip;//点击资源
		private var _width			:int;
		private var _height		:int;
		//数据
		private var _dataArr		:Array	=	[];
		private var _selectItem	:AutoPullUpListItem	=	null;
		private var _hideRoot		:Boolean;		//是否显示不存在子项的跟目录
		private var _hideSelect	:Boolean;		//是否需要选中状态
		private var _rootBgStr		:String = "";	//根项背景资源路径
		private var _layOut		:String = "left";
		private var _treeBg		:Object;		//tree背景    可以是string,从UI库中提取，也可以是自己绘制的显示对象
		private var _curScrollAreaHeight:Number;	//当前滚动区域高度
		private var _itemRootVSpacer:int = 3;		//根项间隙
		private var _curItemRoot	:AutoPullUpListItem;//当前选择根项
		public function AutoPullUpList(w:int,h:int,rootItemBg:String,childItemBg:String,overResStr:String,clickResStr:String,hideRoot:Boolean=false,hideSelect:Boolean=false,rootBgStr:String = "",layOut:String = "left",treeBg:Object=null)
		{
			_width	=	w;
			_height	=	h;
			_rootItemBg = rootItemBg;
			_layOut = layOut;
			_childItemBg = childItemBg;
			_overResStr = overResStr;
			_clickResStr = clickResStr;
			if(rootBgStr != "")
			{
				_rootBgStr = rootBgStr;
			}
			//树背景
			treeBackGround	=	treeBg;
			
			_clickRes = ResManager.newResClass(clickResStr) as MovieClip;
			_clickRes.y = -3;
			
			//初始化显示
			//选中显示
			_hideRoot	=	hideRoot;
			_hideSelect	=	hideSelect;
			addChild(_treeSpace);
			initEvent();
		}
		
		/**
		 *	设置Tree组件的背景 
		 * @param value
		 * 
		 */		
		public function set treeBackGround(value:Object):void
		{
			if(value==null)
			{
				return;
			}
			
			if(value is String)
			{
				var bg:DisplayObject = ResManager.newResClass(String(value)) as DisplayObject;
				bg.cacheAsBitmap;
				bg.y = 1;
				_treeBg	=	bg;
			}
			else if(value is DisplayObject)
			{
				_treeBg	=	value;	//自定义图形
			}
			_treeBg.width		=	_width;	
			_treeBg.height	=	_height;
			addChild(DisplayObject(_treeBg));		
		}
		
		private function initEvent():void
		{
			addEventListener(AutoPullUpListEvent.AUTO_CLICK_ICON, onIconClick);
			addEventListener(AutoPullUpListEvent.AUTO_CLICK_ITEM, onItemClick);
		}
		
		private function onItemClick(evt:AutoPullUpListEvent):void
		{
			if(evt.item.parentNote!=null)
			{
				_selectItem	=	evt.item;//选择子项
				_selectItem.addChildAt(_clickRes,1);//添加选中效果
			}
			else
			{
				_selectItem	=	null;
			}
		}
		
		/**
		 * 获取选择子项 
		 * @return 
		 * 
		 */		
		public function get selectItem():AutoPullUpListItem
		{
			return _selectItem;
		}
		
		public function get selectMcPos():Array
		{
			var tp:Array = new Array();
			tp.push(_clickRes.x);
			tp.push(_clickRes.y);
			return tp;
		}
		
		/**
		 *	Tree节点伸缩 
		 * @param evt
		 * 
		 */		
		private function onIconClick(evt:AutoPullUpListEvent):void
		{
			if(_curItemRoot == evt.item)
			{
				return;
			}
			_curItemRoot = evt.item;
			refreshView();//刷新显示
		}
		
		/**
		 *	获取子项数据 
		 * @return 
		 * 
		 */		
		public function  get dataArr():Array
		{
			return _dataArr;
		}
		
		/**
		 * 添加项(加根项或者根项加子项) 
		 * @param str 项文字内容
		 * @param itemRootIcon 根项图标
		 * @param childIconStr 子项图标
		 * @param pNote 是否是根项
		 * @param id 根id
		 * @param data 根子项数据
		 * @param color 根子项文字颜色
		 * 
		 */		
		public function addItem(str:String,itemRootIcon:String="",childIconStr:String="",pNote:int=-1,id:int=-1,data:Array=null,color:int=0,chidItemBg:String=""):void
		{
			if(pNote==-1)
			{
				var tmpItem	:AutoPullUpListItem	= new AutoPullUpListItem(str,_rootItemBg,_childItemBg,itemRootIcon,childIconStr,_overResStr,true,false);
				tmpItem.id		=	_dataArr.length;	//父项的序列
				tmpItem.data	=	data;
				_dataArr.push(tmpItem);
			}
			else
			{
				var curChildBg:String;
				if(chidItemBg == "")
				{
					curChildBg = _childItemBg;
				}else
				{
					curChildBg = chidItemBg;
				}
				if(color!=0)
				{
					_dataArr[pNote].addItem(str,this,id,data,curChildBg,childIconStr,_overResStr,color);
				}
				else
				{
					_dataArr[pNote].addItem(str,this,id,data,curChildBg,childIconStr,_overResStr);
				}
			}
			refreshView();
		}
		
		public function removeItem(index:int,id:int):void
		{
			if(index<0)return;
			_dataArr[index].removeItemById(id);
			refreshView();
		}
		
		/**
		 * 根据项ID来删除 
		 * @param id
		 * 
		 */
		public function removeItemById(id:int):void
		{
			var tmpItem	:AutoPullUpListItem	=	getItemById(id);
			if(null!=tmpItem.parentNote)
			{
				tmpItem.parentNote.removeItem(tmpItem);
			}
			refreshView();
		}
		
		/**
		 * 更新显示对象 
		 */
		private function refreshView():void
		{
			//清空显示容器
			while(_treeSpace.numChildren>0)
			{
				_treeSpace.removeChildAt(0);
			}
			var hasUsedHeight:int=0;//被根项占用的高度
			for(var i:int=0;i<_dataArr.length;i++)
			{
				if((true==_hideRoot)&&(_dataArr[i].childArr.length==0))
				{
					continue;
				}
				hasUsedHeight += _dataArr[i].iconCkb.height;
			}
			var tmpHeight	:int	=	0;
			var add	: Number = 0;
			for each(var item:AutoPullUpListItem in _dataArr)
			{
				if((true==_hideRoot)&&(item.childArr.length==0))
				{
					continue;
				}
				if(item.iconCkb != null)
				{
					item.hideChild();
					add = item.height;
					if(item == _curItemRoot)
					{
						item.showChild();
						if(item.childSpace.height<(_height-hasUsedHeight))
						{
							item.resizeScrollPanel(item.childSpace.height);
							add += item.childSpace.height;
						}else
						{
							item.resizeScrollPanel(_height-hasUsedHeight);
							add = item.iconCkb.height + _height - hasUsedHeight;
						}
						item.sPanel.update();
					}
					item.y	=	tmpHeight;
					tmpHeight	+=	add;
					_treeSpace.addChild(item);
				}
			}
		}
		/**
		 * 清空List中的数据 
		 * 
		 */		
		public function removeAll():void
		{
			_dataArr=[];
		}
		
		public function clear():void
		{
			_dataArr=[];
			while(_treeSpace.numChildren>0)
			{
				_treeSpace.removeChildAt(0);
			}
		}
		
		public function removeStatus():void
		{
			if(_clickRes.parent!=null)
			{
				_clickRes.parent.removeChild(_clickRes);
			}
		}
		
		/**
		 * 根据根顺序来选定主
		 * @param pIdx
		 * 
		 */
		public function selectFirstRoot():void
		{
			for each(var item:AutoPullUpListItem in _dataArr)
			{
				if(0	==	item.childArr.length)
				{
					continue;					
				}
				item.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
				break;
			}
		}
		
		
		/**
		 * 通过Id选择子项 
		 * @param id
		 * 
		 */		
		public function selectItemById(id:int):void
		{
			for each(var item:AutoPullUpListItem in _dataArr)
			{
				if(0	==	item.childArr.length)
				{
					continue;					
				}
				for each(var childItem:AutoPullUpListItem in item.childArr)
				{
					if(childItem.id==id)
					{
						this.dispatchEvent(new AutoPullUpListEvent(AutoPullUpListEvent.AUTO_CLICK_ICON,item,item.data));
						this.dispatchEvent(new AutoPullUpListEvent(AutoPullUpListEvent.AUTO_CLICK_ITEM,childItem,childItem.data));
					}
				}
			}
		}
		
		public function selectItemByData(data:String):void
		{
			for each(var item:AutoPullUpListItem in _dataArr)
			{
				if(0	==	item.childArr.length)
				{
					continue;					
				}
				for each(var childItem:AutoPullUpListItem in item.childArr)
				{
					if(StringUtil.contains((childItem.htmlText),data)>0)
					{
						this.dispatchEvent(new AutoPullUpListEvent(AutoPullUpListEvent.AUTO_CLICK_ICON,item,item.data));
						this.dispatchEvent(new AutoPullUpListEvent(AutoPullUpListEvent.AUTO_CLICK_ITEM,childItem,childItem.data));
					}
				}
			}
		}
		
		public function getItemById(id:int):AutoPullUpListItem
		{
			for each(var item:AutoPullUpListItem in _dataArr)
			{
				if(0	==	item.childArr.length)
				{
					continue;					
				}
				for each(var childItem:AutoPullUpListItem in item.childArr)
				{
					if(childItem.id == id)
					{
						return childItem;
					}
				}
			}
			return null;
		}
		
		public function get curItemRoot():AutoPullUpListItem
		{
			return _curItemRoot;
		}
	}
}